#include "../include/http_request.h"
#include <stdexcept>

namespace http {

HttpRequest::HttpRequest() : _version("HTTP/1.1") {}

void HttpRequest::reSet() 
{
    _method.clear();
    _path.clear();
    _version = "HTTP/1.1";
    _body.clear();
    std::smatch().swap(_matches); // 清空正则匹配结果
    _headers.clear();
    _params.clear();
}

size_t HttpRequest::contentLength() const 
{
    if (!hasHeader("Content-Length")) 
    {
        return 0;
    }
    try 
    {
        return std::stol(getHeader("Content-Length"));
    } 
    catch (const std::exception& e) 
    {
        return 0;
    }
}

bool HttpRequest::close() const 
{
    // HTTP/1.0默认短连接，HTTP/1.1默认长连接
    if (_version == "HTTP/1.0") 
    {
        return !hasHeader("Connection") || getHeader("Connection") != "keep-alive";
    }
    return hasHeader("Connection") && getHeader("Connection") == "close";
}

} // namespace http